package com.ews.onlineexchange.service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Date;

import javax.ejb.EJBException;
import javax.enterprise.context.ConversationScoped;
import javax.inject.Inject;
import javax.inject.Named;

import com.ews.onlineexchange.model.Bulletin;
import com.ews.onlineexchange.model.ServiceSupplier;
import com.ews.onlineexchange.model.TradeRate;

/**
 * @author ALIENWARE
 *
 */
@Named
@ConversationScoped
public class TradeRateHome extends EntityHome<TradeRate, String> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Inject
	TradeHome tradeHome;
	
	/**
	 * 防止恶意通过修改URL对无关中介进行评价
	 */
	public void validateTrade() {
		if (!tradeHome.getInstance().getBulletin().isOver()) {
			throw new EJBException();
		}
		
		if (!tradeHome.getInstance().isWinner()) {
			throw new EJBException();
		}
	}
	
//	@Override
//	public TradeRate createInstance() {		
//		TradeRate instance = super.createInstance();
//		
//		return instance;
//	}

	@Override
	public String saveAndFlush() {		
		Integer totalScore =  getInstance().getAttitudeRate() 
							+ getInstance().getPriceRate()
							+ getInstance().getStandardRate() 
							+ getInstance().getTimeRate() 
							+ getInstance().getQualityRate();
		BigDecimal overAllScore = new BigDecimal(totalScore / 5);
		getInstance().setOverallScore(overAllScore);
		if(!isManaged()) {
			getInstance().setTrade(tradeHome.getInstance());
		}
		
		Bulletin bulletin = getInstance().getTrade().getBulletin();
		bulletin.setDone(true);
		bulletin.setDoneTime(new Date());
		getEntityManager().merge(bulletin);
		
		ServiceSupplier  supplier = getInstance().getTrade().getSupplier();
		supplier.setOrderCount(supplier.getOrderCount() + 1);
		//&& supplier.getScore().compareTo(BigDecimal.ZERO) == 0
		if(null == supplier.getScore() ) {
			supplier.setScore(getInstance().getOverallScore());
		}
		else {
			supplier.setScore((getInstance().getOverallScore().add(supplier.getScore()).divide(new BigDecimal(2))));
		}
		getEntityManager().merge(supplier);
		String result = super.saveAndFlush();
		
		
		return result;
	}
	
	
	
	/**管理员修改恶意评价
	 * @return
	 */
	public String updateGrade() {	
		//先获取之前的评分
		BigDecimal priorOverAllScore = getInstance().getOverallScore();
		
		DecimalFormat df = new DecimalFormat("0.0");
		Integer totalScore =  getInstance().getAttitudeRate() 
							+ getInstance().getPriceRate()
							+ getInstance().getStandardRate() 
							+ getInstance().getTimeRate() 
							+ getInstance().getQualityRate();
		BigDecimal totalScoreDec = new BigDecimal(totalScore);
		//最新一次修改后的评分
		BigDecimal overAllScore = totalScoreDec.divide(new BigDecimal(5));
		
		getInstance().setOverallScore(overAllScore.setScale(1,BigDecimal.ROUND_CEILING));
		
		ServiceSupplier  supplier = getInstance().getTrade().getSupplier();
		//目前中介的综合评分
		BigDecimal priorSupplierScore = supplier.getScore();
		//中介原本的评分
		BigDecimal rightScore = priorSupplierScore.multiply(new BigDecimal(2)).subtract(priorOverAllScore);
		
		//除去上次评价的分数，重新计算
		supplier.setScore((getInstance().getOverallScore().add(rightScore).divide(new BigDecimal(2))).setScale(1,BigDecimal.ROUND_CEILING));
		
		getEntityManager().merge(supplier);
		String result = super.saveAndFlush();
		
		
		return result;
	}
	
}
